package cn.wolfcode;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.List;

public class ActivitiTest2 {
    // 初始化
    @Test
    public void a(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

    // 部署
    @Test
    public void b(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/leaveProcess.bpmn20.xml")
                .deploy();
        System.out.println("部署ID = " + deployment.getId());
        System.out.println("部署Time = " + deployment.getDeploymentTime());

    }

    // 发起流程实例
    @Test
    public void c(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("leaveProcess");
        System.out.println("流程实例ID = " + processInstance.getId());
        System.out.println("流程实例ID = " + processInstance.getProcessInstanceId());
        System.out.println("流程实例对应的流程定义ID = " + processInstance.getProcessDefinitionId());
        System.out.println("流程实例对应的流程定义Key = " + processInstance.getProcessDefinitionKey());
    }


    // 查询代办任务
    @Test
    public void d(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String userId="张三";
        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("leaveProcess")
                .taskAssignee(userId)
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID = " + task.getId());
            System.out.println("任务名称 = " + task.getName());
            System.out.println("任务负责人 = " + task.getAssignee());
            System.out.println("任务创建时间 = " + task.getCreateTime());
            System.out.println("任务对应的流程定义ID = " + task.getProcessDefinitionId());
            System.out.println("任务对应的流程实例ID = " + task.getProcessInstanceId());

        }

    }

    // 执行任务（处理任务）
    @Test
    public void e(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId="2505";
         taskService.complete(taskId);

    }

    // 添加审批意见
    @Test
    public void f(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId="5002";
        String processInstanceId="2501";
        taskService.addComment(taskId,processInstanceId,"小豪也想休假");
        taskService.complete(taskId);
    }

    // 查询历史任务信息
    @Test
    public void g(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        TaskService taskService = processEngine.getTaskService();
        String processInstanceId="2501";
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .processInstanceId(processInstanceId)
                .finished()
                .list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("任务ID = " + historicTaskInstance.getId());
            System.out.println("任务名称 = " + historicTaskInstance.getName());
            System.out.println("任务负责人 = " + historicTaskInstance.getAssignee());
            System.out.println("任务创建时间 = " + historicTaskInstance.getCreateTime());
            System.out.println("任务开始时间 = " + historicTaskInstance.getStartTime());
            System.out.println("任务结束时间 = " + historicTaskInstance.getEndTime());
            System.out.println("任务对应的流程定义ID = " + historicTaskInstance.getProcessDefinitionId());
            System.out.println("任务对应的流程实例ID = " + historicTaskInstance.getProcessInstanceId());
            List<Comment> taskComments = taskService.getTaskComments(historicTaskInstance.getId());
            if(taskComments!=null && !taskComments.isEmpty()){
                for (Comment taskComment : taskComments) {
                    System.out.println(" 批注信息= " + taskComment.getFullMessage());
                }
            }
            System.out.println("❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤");
        }

    }
}
